//
//  CYListTableView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/27.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYListTableView.h"
@interface CYListTableView()<UITableViewDelegate , UITableViewDataSource>
@end
@implementation CYListTableView
- (instancetype)initWithArray:(NSArray *)array{
    self = [super init];
    if (self) {
        self.array = array;
        self.delegate = self;
        self.dataSource = self;
        [self setSeparatorInset:UIEdgeInsetsZero];
        [self setLayoutMargins:UIEdgeInsetsZero];
        self.bounces = NO;
        [self setupLayer];
    }
    return self;
}

- (void)setupLayer{
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = self.separatorColor.CGColor;
    self.layer.masksToBounds = YES;
}

- (void)setupTranform{
    [UIView animateWithDuration:0.3 animations:^{
        if (self.hidden == YES)
            self.hidden = NO;
        self.transform = CGAffineTransformIdentity;
    }];
}
#pragma mark - UITableViewDataSource - method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

static NSString *identifier = @"UITableViewCell";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    __weak __typeof(&*self)weakSelf = self;
    cell.textLabel.text = weakSelf.array[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate - method
-(void)tableView:(UITableView* )tableView willDisplayCell:(UITableViewCell* )cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    /*设置cell的分割线的靠左边距离0对齐*/
    [cell setSeparatorInset:UIEdgeInsetsZero];
    [cell setLayoutMargins:UIEdgeInsetsZero];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectedBackgroundView = [[CYView alloc]initWithColor:RGBA(240, 55, 67, 0.1)];
   
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
    __weak __typeof(&*self)weakSelf = self;
    [weakSelf setupTranform];
    
    [weakSelf.listTableDelegate dataForSelectCell:weakSelf.array[indexPath.row] indexPath:indexPath];
}
@end
